package com.atguigu.gmall.payment.service.impl;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.enums.ProcessStatus;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitService rabbitService;

    @Override
    public void savePaymentInfo(OrderInfo orderInfo, String paymentType) {
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("order_id", orderInfo.getId());
        paymentInfoQueryWrapper.eq("out_trade_no", orderInfo.getOutTradeNo());
        paymentInfoQueryWrapper.eq("payment_type", paymentType);
        Integer count = paymentInfoMapper.selectCount(paymentInfoQueryWrapper);
//        大于0说明有数据将不再插入数据 直接返回
        if (count > 0) {
            return;
        }

        //创建paymentInfo
        PaymentInfo paymentInfo = new PaymentInfo();
        //给paymentInfo赋值
        paymentInfo.setCreateTime(new Date());//设置创建时间
        paymentInfo.setOrderId(orderInfo.getId());//设置订单id
        paymentInfo.setPaymentType(paymentType);//支付方式
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());//交易编号
        paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());//支付的状态
        paymentInfo.setSubject(orderInfo.getTradeBody());
        //paymentInfo.setSubject("test");
        paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
        //保存paymentInfo
        paymentInfoMapper.insert(paymentInfo);
    }

    @Override
    public PaymentInfo getPaymentInfo(String outTradeNo, String paymentType) {
        //查询数据
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("out_trade_no", outTradeNo);
        paymentInfoQueryWrapper.eq("payment_type", paymentType);
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(paymentInfoQueryWrapper);

        if (paymentInfo != null) {
            return paymentInfo;
        }
        return null;
    }

    //支付成功
    @Override
    public void paySuccess(String outTradeNo, String paymentType, Map<String, String> paramsMap) {
        PaymentInfo  paymentInfoQuery = this.getPaymentInfo(outTradeNo, paymentType);
        if (paymentInfoQuery==null){
            return;
        }
        try {
            //创建对象
            PaymentInfo paymentInfo = new PaymentInfo();
            //赋值
            paymentInfo.setCallbackTime(new Date());
            paymentInfo.setCallbackContent(paramsMap.toString());
            paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
            paymentInfo.setTradeNo(paramsMap.get("trade_no"));
            this.updatePaymentInfo(outTradeNo, paymentType, paymentInfo);
        } catch (Exception e) {
            //有异常删除key
            redisTemplate.delete(paramsMap.get("notify_id"));
            e.printStackTrace();
        }
        //发送消息
        rabbitService.sendMessage(
                MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,
                MqConst.ROUTING_PAYMENT_PAY,
                paymentInfoQuery.getOrderId());
    }

    //更新方法
    public void updatePaymentInfo(String outTradeNo, String paymentType, PaymentInfo paymentInfo) {
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("out_trade_no", outTradeNo);
        paymentInfoQueryWrapper.eq("payment_type", paymentType);
        //调用更新方法
        paymentInfoMapper.update(paymentInfo, paymentInfoQueryWrapper);

    }

    @Override
    public void closePayment(Long orderId) {
        //创建支付信息的对象
        PaymentInfo paymentInfo = new PaymentInfo();
        //更新交易状态
        paymentInfo.setPaymentStatus(ProcessStatus.CLOSED.name());
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("order_id",orderId);
        paymentInfoMapper.update(paymentInfo,paymentInfoQueryWrapper);
    }
}